package com.bootdo.kaoqin.queue;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.kaoqin.domain.KaoqinConfig;
import com.bootdo.kaoqin.domain.KaoqinOrder;
import com.bootdo.kaoqin.domain.KaoqinUser;
import com.bootdo.kaoqin.model.WorkTimeModel;
import com.bootdo.kaoqin.service.KaoqinCallGroupService;
import com.bootdo.kaoqin.service.KaoqinConfigService;
import com.bootdo.kaoqin.service.KaoqinOrderService;
import com.bootdo.kaoqin.service.KaoqinUserService;
import com.bootdo.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

//@Service
public class CallWorkQueue implements Runnable{

    private volatile KaoqinOrderService kaoqinOrderService;

    private volatile KaoqinCallGroupService kaoqinCallGroupService;

    private volatile KaoqinConfigService kaoqinConfigService;

    private volatile KaoqinUserService kaoqinUserService;

    private volatile   RedisUtil redisUtil;

    @Autowired
    public CallWorkQueue(KaoqinOrderService kaoqinOrderService,
                         KaoqinCallGroupService kaoqinCallGroupService,
                         KaoqinConfigService kaoqinConfigService,
                         KaoqinUserService kaoqinUserService,
                         RedisUtil redisUtil
                         ){
        this.kaoqinCallGroupService = kaoqinCallGroupService;
        this.kaoqinConfigService = kaoqinConfigService;
        this.kaoqinOrderService = kaoqinOrderService;
        this.kaoqinUserService = kaoqinUserService;
        this.redisUtil = redisUtil;
        new Thread(this).start();
    }


    public static LinkedBlockingQueue<KaoqinUser> kaoqinUserLinkedBlockingQueue = new LinkedBlockingQueue<>();

    @Override
    public void run() {
        while (true){
            try {
                System.err.println("打卡处理系统准备等待执行,,,,,,,,,");
                KaoqinUser kaoqinUser =   kaoqinUserLinkedBlockingQueue.take();
                //if(redisUtil.get("CALL_CATCH_"+kaoqinUser.getId()) != null){
                    callHander(kaoqinUser);
                    redisUtil.set("CALL_CATCH_"+kaoqinUser.getId(), "ok",120);
                //}
                System.err.println("打卡处理系统准备执行完毕,,,,,,,,,");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    /**
     * 考勤处理
     * @param kaoqinUser
     */
    public void callHander(KaoqinUser kaoqinUser){
        Date NOW =   new Date();
        boolean isHaveNow = false;
        int index = 0;
        List<KaoqinConfig> kaoqinConfigs =  kaoqinConfigService
                .listByNowTimeNoEnd(DateUtils.format(NOW,"HH:mm:ss"),
                        kaoqinUser.getUserId(),kaoqinUser.getGroupId());
        //遍历出考勤组下的时间   如果结束时间大于当前时间  就把结束时间+1
        for(KaoqinConfig kaoqinConfig:kaoqinConfigs){
            kaoqinConfig.setStartTime(DateUtils.getCustomTimeHms(kaoqinConfig.getStartTime()));
            kaoqinConfig.setEndTime(DateUtils.getCustomTimeHms(kaoqinConfig.getEndTime()));
            if(kaoqinConfig.getStartTime().after(kaoqinConfig.getEndTime())){
                kaoqinConfig.setEndTime(DateUtils.getCustomTimeHmsAdd1(kaoqinConfig.getEndTime()));
            }
            if(kaoqinConfig.getStartTime().before(NOW) && kaoqinConfig.getEndTime().after(NOW)){
                isHaveNow = true;
                break;
            }
            ++index;
        }
        //if(null != kaoqinConfigs && kaoqinConfigs.size() > 0){
        if(isHaveNow){
            KaoqinOrder kaoqinOrder  =   kaoqinOrderService.getByNowTimeAndConfigId(DateUtils.format(NOW,DateUtils.DATE_PATTERN),kaoqinConfigs.get(index).getId(),kaoqinUser.getId(),0);
            if(null == kaoqinOrder ){
                kaoqinOrder = new KaoqinOrder();
            }
            kaoqinOrder.setCityId(kaoqinUser.getCityId());
            kaoqinOrder.setCityName(kaoqinUser.getCityName());
            kaoqinOrder.setClassId(kaoqinUser.getClassId());
            kaoqinOrder.setClassName(kaoqinUser.getClassName());
            kaoqinOrder.setCreateDate(NOW);
            kaoqinOrder.setCreateTime(NOW);
            kaoqinOrder.setCreateTimeTime(NOW);
            kaoqinOrder.setEmpId(kaoqinUser.getId());
            kaoqinOrder.setEmpName(kaoqinUser.getName());
            kaoqinOrder.setUserId(kaoqinUser.getUserId());
            kaoqinOrder.setUserName(kaoqinUser.getUserName());
            kaoqinOrder.setIsTimeout(0);
            kaoqinOrder.setType(0);
            kaoqinOrder.setMobile(kaoqinUser.getMobile());
            kaoqinOrder.setGroupId(kaoqinConfigs.get(index).getGroupId());
            kaoqinOrder.setGroupName(kaoqinConfigs.get(index).getGroupName());
            kaoqinOrder.setConfigId(kaoqinConfigs.get(index).getId());
            kaoqinOrder.setConfigName(kaoqinConfigs.get(index).getName());
            kaoqinOrder.setIsTimeout(0);
            kaoqinOrder.setTimeoutNum(0);
            kaoqinOrderService.save(kaoqinOrder);
        }else{
            noInCallTimeHander(kaoqinUser,NOW);
        }
    }

    /**
     * 如果没在工作时间端打卡的处理
     * @param kaoqinUser
     * @param NOW
     */
    void noInCallTimeHander(KaoqinUser kaoqinUser, Date NOW){
        List<WorkTimeModel> workTimeModels =  new ArrayList<>();
        List<KaoqinConfig> kaoqinConfigs =  kaoqinConfigService
                .list(MapBuild.getInstand()
                        .setKV("groupId",kaoqinUser.getGroupId())
                );
        if(kaoqinConfigs.size() % 2 != 0){
            return;
        }
        for(int i = 0 ; i < kaoqinConfigs.size() ; i++){
            KaoqinConfig startTime = kaoqinConfigs.get(i);
            // 1是下班  0是上班
            if(startTime.getType().intValue() == 0){
                int temp = i+1;
                if(temp <= kaoqinConfigs.size()){
                    KaoqinConfig endTime = kaoqinConfigs.get(temp);
                    WorkTimeModel workTimeModel = new WorkTimeModel();
                    workTimeModel.setStartTime(DateUtils.getCustomTimeHms(startTime.getEndTime()));
                    workTimeModel.setEndTime(DateUtils.getCustomTimeHms(endTime.getStartTime()));
                    if(workTimeModel.getStartTime().after(workTimeModel.getEndTime())){
                        workTimeModel.setEndTime(DateUtils.getCustomTimeHmsAdd1(endTime.getStartTime()));
                    }
                    workTimeModel.setStartConfig(startTime);
                    workTimeModel.setEndConfig(endTime);
                    workTimeModels.add(workTimeModel);
                }
            }
        }

        //如果没取到工作时间 就返回
        if(workTimeModels.size() <= 0){
            return;
        }

        for(int i = 0; i < workTimeModels.size(); i++){
            WorkTimeModel workTimeModel = workTimeModels.get(i);
            //before 测试此日期是否是在指定日期之前
            //在此日期之前  和  之后
            //如果进入了  说明在当前工作时间内
            if(workTimeModel.getStartTime().before(NOW) && workTimeModel.getEndTime().after(NOW)){

                KaoqinOrder kaoqinOrder = new KaoqinOrder();
                kaoqinOrder.setCityId(kaoqinUser.getCityId());
                kaoqinOrder.setCityName(kaoqinUser.getCityName());
                kaoqinOrder.setClassId(kaoqinUser.getClassId());
                kaoqinOrder.setClassName(kaoqinUser.getClassName());
                kaoqinOrder.setCreateDate(NOW);
                kaoqinOrder.setCreateTime(NOW);
                kaoqinOrder.setCreateTimeTime(NOW);
                kaoqinOrder.setEmpId(kaoqinUser.getId());
                kaoqinOrder.setEmpName(kaoqinUser.getName());
                kaoqinOrder.setUserId(kaoqinUser.getUserId());
                kaoqinOrder.setUserName(kaoqinUser.getUserName());
                kaoqinOrder.setMobile(kaoqinUser.getMobile());

                //上班卡记录
                KaoqinOrder upOrder  =   kaoqinOrderService.getByNowTimeAndConfigId(DateUtils.format(NOW,DateUtils.DATE_PATTERN),workTimeModel.getStartConfig().getId(),kaoqinUser.getId(),0);
                if(null == upOrder){
                    upOrder = kaoqinOrderService.getByNowTimeAndConfigId(DateUtils.format(NOW,DateUtils.DATE_PATTERN),workTimeModel.getStartConfig().getId(),kaoqinUser.getId(),2);
                }
                //说明打过上班卡  那就是早退卡
                if(upOrder != null){
                    //下班卡记录 也存在 就更新下下班卡记录
                    KaoqinOrder downOrder  =   kaoqinOrderService.getByNowTimeAndConfigId(DateUtils.format(NOW,DateUtils.DATE_PATTERN),workTimeModel.getEndConfig().getId(),kaoqinUser.getId(),1);
                    if(null != downOrder){
                        kaoqinOrder = downOrder;
                        kaoqinOrder.setCreateDate(NOW);
                        kaoqinOrder.setCreateTime(NOW);
                        kaoqinOrder.setCreateTimeTime(NOW);
                    }else{
                        //如果是迟到卡 就更新一下迟到时间  反之创建一个早退卡
                        if(upOrder.getType().intValue() == 2){
                            kaoqinOrder = upOrder;
                            kaoqinOrder.setCreateDate(NOW);
                            kaoqinOrder.setCreateTime(NOW);
                            kaoqinOrder.setCreateTimeTime(NOW);
                        }else{
                            kaoqinOrder.setType(1);
                            kaoqinOrder.setCreateDate(NOW);
                            kaoqinOrder.setCreateTime(NOW);
                            kaoqinOrder.setCreateTimeTime(NOW);
                            kaoqinOrder.setGroupId(workTimeModel.getEndConfig().getGroupId());
                            kaoqinOrder.setGroupName(workTimeModel.getEndConfig().getGroupName());
                            kaoqinOrder.setConfigId(workTimeModel.getEndConfig().getId());
                            kaoqinOrder.setConfigName(workTimeModel.getEndConfig().getName());
                        }
                    }
                }else{
                    //反之就是迟到卡
                    kaoqinOrder.setCreateDate(NOW);
                    kaoqinOrder.setCreateTime(NOW);
                    kaoqinOrder.setCreateTimeTime(NOW);
                    kaoqinOrder.setGroupId(workTimeModel.getStartConfig().getGroupId());
                    kaoqinOrder.setGroupName(workTimeModel.getStartConfig().getGroupName());
                    kaoqinOrder.setConfigId(workTimeModel.getStartConfig().getId());
                    kaoqinOrder.setConfigName(workTimeModel.getStartConfig().getName());
                    kaoqinOrder.setType(2);
                }
                kaoqinOrderService.save(kaoqinOrder);
            }

        }

    }
}
